package uebString;
 
import java.util.*;
import java.io.*;
import java.util.Scanner;

import exceptions.CanNotReadException;
import exceptions.CanNotWriteException;
import exceptions.IsNoFileException;
import exceptions.StringIsEmptyException;

/**
 * Class StringReader
 * 
 * @author Andreas Dier, Steffen Schuhmann 
 * @version 0.3
 */
public class StringReader {

	private LineNumberReader in;

	public StringReader(String fileName) throws IOException, StringIsEmptyException, IsNoFileException, CanNotReadException, CanNotWriteException {
		if(fileName == null)
			throw new NullPointerException("Given String is null");
		if(fileName.equals(""))
			throw new StringIsEmptyException("Given String is empty");
			
		File file = new File(fileName);
		
		if(!file.exists())
			throw new FileNotFoundException("This file was not found");
		if(!file.isFile()) 
			throw new IsNoFileException("This is not a file");
		if(!file.canRead())
			throw new CanNotReadException("You do not have read access for this file");
		if(!file.canWrite())
			throw new CanNotWriteException("You do not have write access for this file");
			
        in = new LineNumberReader(new BufferedReader(new FileReader(fileName)));     
    }

	
	public String read(int lineNumber) throws IOException {
        String lineString;
        try {
				in.setLineNumber(lineNumber);
				return lineString = in.readLine();
            
        } catch (RuntimeException e) {
            throw new IOException("Fehler beim Einlesen", e);
        } 
    }

    public void close() throws IOException {
        in.close();
        in = null;
    }

    protected void finalize() throws Throwable {
        super.finalize();
        if (in != null)
            close();
    }
}
